
<script type="text/javascript">
    $(document).ready(function(){
        $(document).on('change','.bt_choice_image',function(evt){
            //$('.bt_choice_image').change(function(evt){
            var dis = $(this);
            var f = evt.target.files[0];
            var reader = new FileReader();

            if (!f.type.match('image.*')) {
                alert("The selected file does not appear to be an image.");
                return;
            }
            reader.onload = function (e) {
                var img = $('<img/>');
                img.attr("src", e.target.result).load(function () {
                    //$('.image_preview').attr("src",e.target.result);
                    dis.closest('.dangspdivimg').find('img').attr("src",e.target.result);
                });

            };
            reader.readAsDataURL(f);
        }); 

        // Add image panel
        $('#bt_add_image').click(function(){
            var sHtml = '<div class="dangspdivimg">';
            sHtml +=        '<img class="image_preview" src="' + $('#path').val() + 'image/big_default_picture" alt="">';
            sHtml +=        '<input type="file" name="avatar[]" class="bt_choice_image dangspinputfile">';
            sHtml +=    '</div>';
            $('.panel_add_image').before(sHtml);
        });
    });
    // Function change select
    function onchangeSelect(){
        $('#dm_name_id').val($("select").val());
    }

    // upload complete
    function uploadComplete(link){
        if(!$('#form_page').dialog("isOpen")){
            window.location = link;
        }
        else{
            $('#form_page input[type="text"]').val('');
            $('#form_page textarea').val('');
            $('#successfull_message').css('display','block');
            $('#form_page').scrollTop(0);
        }
    }
</script>
<?php
    if(isset($template->aParams['result']))
    {
        echo '<div class="metro_message_susscess"><i class="fa fa-check"></i> Cập nhập sản phẩm thành công!</div>';
    }
?>

<h2 id="successfull_message" style="display: none;" class="title green_style">CẬP NHẬP SẢN PHẨM THÀNH CÔNG!</h2>
<form action="" method="POST" enctype="multipart/form-data">
    <div class="left_product_content">
        <div class="dangspdivimg">
            <img class="image_preview" src="<?php echo URL::getLink('upload/'.$template->get('defaultImage')); ?>" alt="">
            <input type="file" name="avatar[]" class="bt_choice_image dangspinputfile">
        </div>
        <div class="panel_add_image" style="text-align: center;padding-top:20px;clear: both;">
            <button id="bt_add_image" class="flat_metro_button violet_metro_style" type="button"><i class="fa fa-picture-o"></i> Thêm hình <i class="fa fa-plus"></i></button>
        </div>
    </div>
    <div class="right_product_content">
        <div class="dangsptitle">
            <div style="margin-left: 20px;">
                <div>
                    <p>Tiêu  Đề</p>
                    <p><input value="<?php echo $template->get('name'); ?>" type="text" name="tensp" style="width: 525px;"></p>
                </div>
                <div>
                    <p>Nhà Sản Xuất</p>
                    <?php
                        $aParams = array(
                            'width' => '500px',
                            'id' =>'tb_nhasanxuat',
                            'aValue' => DBObject::getManufacturers(),
                            'stringTitle' => 'name',
                            'stringKey' => 'uri',
                            'value' => $template->get('menufacturerUri'),
                            'title' => $template->get('menufacturerName'),
                        );
                        includeBlock('block/customComboboxBlock',$aParams);
                    ?>
                    <p>Đơn Giá</p>
                    <p><input value="<?php echo $template->get('price'); ?>" type="text" name="dongia" style="width:525px;"></p>
                    <p> Địa Điểm :
                        <?php
                            $aParamLocation = array(
                                'width' => '500px',
                                'id' =>'location_id',
                                'aValue' => DBObject::getLocations(),
                                'stringTitle' => 'name',
                                'stringKey' => 'name',
                                'value' => $template->get('location'),
                                'title' => $template->get('location'),
                            );
                            includeBlock('block/customComboboxBlock',$aParamLocation);
                        ?>
                    </p>
                    <p>Danh Mục</p>
                    <?php
                        $aParams = array(
                            'width' => '500px',
                            'category_uri' => $template->get('categoryUri'),
                            'category_name' => $template->get('categoryName'),
                            'list_categories' => $template->get('list_categories'),
                        );
                        includeBlock('block/categoryBlock' , $aParams);
                    ?>
                </div>
                <div> 
                    <p>Mô tả chi tiết</p>
                    <textarea class="" id="summary" name="description" cols="20" rows="20" style="width: 540px;" ><?php echo $template->get('description'); ?></textarea>
                </div>
            </div>
            <div style="text-align: right;margin-bottom:10px;clear: both;">
                <input type="text" hidden="true" name="title">
                <button type="submit dangspbutton" class="flat_metro_button green_metro_style">Cập nhập sản phẩm<i class="fa fa-plus"></i></button>
            </div>
        </div>
    </div>
    <div style="clear: both;"></div>
</form>
<iframe id="upload_target" name="upload_target" src="" style="display: none;"></iframe>